package com.liuzy.crawler;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import com.liuzy.crawler.core.MyListener;

/**
 * @author liuzy
 * @since 2015年9月23日
 */
public class Setting {
	static Shell setting;
	static TabItem tabB, tabC, tabS, tabD, tabO;
	static Composite cpBasic, cpCache, cpScan, cpDl, cpAdvance;
	static Label lblCacheSize, lblCacheCount;
	static Text txtCacheSize, txtCacheCount;
	static Label lblScanSize, lblScanCount;
	static Text txtScanSize, txtScanCount;
	static Label lblDlSize, lblDlCount;
	static Text txtDlSize, txtDlCount;
	static Label lblDlDir;
	static Button ckAutoCacheDir;
	static Text txtDllDir, txtCacheDir;
	static Button btnDlDir, btnCacheDir;
	static Button ckCacheAutoClear, ckScanDelFile;
	static Button ckDlBySite, ckDlRename, ckDlFast;
	static Button checkVersion, autoUpdate, update;
	private static Label lblesc;

	/**
	 * @wbp.parser.entryPoint
	 */
	static void show(Shell shell, char ch) {
		setting = new Shell(shell, SWT.PRIMARY_MODAL);
		setting.setSize(400, 300);
		setting.setText("设置");
		setting.setLocation(shell.getLocation().x + setting.getSize().x / 2, shell.getLocation().y + setting.getSize().y / 2);
		
		lblesc = new Label(setting, SWT.NONE);
		lblesc.setLocation(335, 0);
		lblesc.setSize(63, 17);
		lblesc.setText("按ESC退出");

		Composite composite = new Composite(setting, SWT.NONE);
		composite.setBounds(10, 10, 378, 278);

		TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
		tabFolder.setBounds(0, 0, 378, 278);

		tabB = new TabItem(tabFolder, SWT.NONE);
		tabB.setText("基本(&B)");

		cpBasic = new Composite(tabFolder, SWT.NONE);
		tabB.setControl(cpBasic);

		lblDlDir = new Label(cpBasic, SWT.NONE);
		lblDlDir.setBounds(39, 32, 80, 17);
		lblDlDir.setText("下载保存路径");

		txtDllDir = new Text(cpBasic, SWT.BORDER | SWT.READ_ONLY);
		txtDllDir.setBounds(39, 55, 226, 23);

		btnDlDir = new Button(cpBasic, SWT.NONE);
		btnDlDir.setBounds(271, 53, 63, 27);
		btnDlDir.setText("选择...");
		btnDlDir.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog openDir = new DirectoryDialog(setting, SWT.OPEN);
				openDir.setText("选择下载目录...");
				String dir = openDir.open();
				if (dir != null) {
					txtDllDir.setText(dir);
					if (ckAutoCacheDir.getSelection()) {
						txtCacheDir.setText(dir + "\\_cache");
					}
				}
			}
		});

		txtCacheDir = new Text(cpBasic, SWT.BORDER | SWT.READ_ONLY);
		txtCacheDir.setBounds(39, 128, 226, 23);

		btnCacheDir = new Button(cpBasic, SWT.NONE);
		btnCacheDir.setEnabled(false);
		btnCacheDir.setText("选择...");
		btnCacheDir.setBounds(271, 126, 63, 27);
		btnCacheDir.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog openDir = new DirectoryDialog(setting, SWT.OPEN);
				openDir.setText("选择缓存目录...");
				String dir = openDir.open();
				if (dir != null) {
					txtCacheDir.setText(dir);
				}
			}
		});

		ckAutoCacheDir = new Button(cpBasic, SWT.CHECK);
		ckAutoCacheDir.setSelection(true);
		ckAutoCacheDir.setBounds(39, 105, 117, 17);
		ckAutoCacheDir.setText("自动设置缓存目录");
		ckAutoCacheDir.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (ckAutoCacheDir.getSelection()) {
					btnCacheDir.setEnabled(false);
					txtCacheDir.setText(txtDllDir.getText() + "_cache");
				} else {
					btnCacheDir.setEnabled(true);
				}
			}
		});

		tabC = new TabItem(tabFolder, SWT.NONE);
		tabC.setText("缓存池(&C)");

		cpCache = new Composite(tabFolder, SWT.NONE);
		tabC.setControl(cpCache);

		lblCacheSize = new Label(cpCache, SWT.NONE);
		lblCacheSize.setBounds(63, 27, 134, 17);
		lblCacheSize.setText("缓存任务队列大小(≥100)");

		lblCacheCount = new Label(cpCache, SWT.NONE);
		lblCacheCount.setText("同时下载最大任务数(1-100)");
		lblCacheCount.setBounds(63, 71, 149, 17);

		txtCacheSize = new Text(cpCache, SWT.BORDER | SWT.RIGHT);
		txtCacheSize.setBounds(216, 22, 90, 23);
		MyListener.addNumberVerifyListener(txtCacheSize, 100, Integer.MAX_VALUE);

		txtCacheCount = new Text(cpCache, SWT.BORDER | SWT.RIGHT);
		txtCacheCount.setBounds(216, 67, 90, 23);
		MyListener.addNumberVerifyListener(txtCacheCount, 1, 100);

		ckCacheAutoClear = new Button(cpCache, SWT.CHECK);
		ckCacheAutoClear.setText("自动删除缓存");
		ckCacheAutoClear.setSelection(true);
		ckCacheAutoClear.setBounds(63, 116, 98, 17);

		tabS = new TabItem(tabFolder, SWT.NONE);
		tabS.setText("扫描池(&S)");

		cpScan = new Composite(tabFolder, SWT.NONE);
		tabS.setControl(cpScan);

		lblScanSize = new Label(cpScan, SWT.NONE);
		lblScanSize.setText("扫描任务队列最大值(≥100)");
		lblScanSize.setBounds(63, 28, 147, 17);

		lblScanCount = new Label(cpScan, SWT.NONE);
		lblScanCount.setText("同时扫描线程数量(1-50)");
		lblScanCount.setBounds(63, 72, 135, 17);

		txtScanSize = new Text(cpScan, SWT.BORDER | SWT.RIGHT);
		txtScanSize.setBounds(216, 22, 90, 23);
		MyListener.addNumberVerifyListener(txtScanSize, 100, Integer.MAX_VALUE);

		txtScanCount = new Text(cpScan, SWT.BORDER | SWT.RIGHT);
		txtScanCount.setBounds(216, 67, 90, 23);
		MyListener.addNumberVerifyListener(txtScanCount, 1, 50);

		ckScanDelFile = new Button(cpScan, SWT.CHECK);
		ckScanDelFile.setSelection(true);
		ckScanDelFile.setBounds(63, 116, 141, 17);
		ckScanDelFile.setText("扫描完成后删除源文件");

		tabD = new TabItem(tabFolder, SWT.NONE);
		tabD.setText("下载池(&D)");

		cpDl = new Composite(tabFolder, SWT.NONE);
		tabD.setControl(cpDl);

		lblDlSize = new Label(cpDl, SWT.NONE);
		lblDlSize.setText("下载任务队列大小(≥100)");
		lblDlSize.setBounds(63, 26, 139, 17);

		lblDlCount = new Label(cpDl, SWT.NONE);
		lblDlCount.setText("同时下载最大任务数(1-100)");
		lblDlCount.setBounds(63, 70, 149, 17);

		txtDlSize = new Text(cpDl, SWT.BORDER | SWT.RIGHT);
		txtDlSize.setBounds(216, 22, 90, 23);
		MyListener.addNumberVerifyListener(txtDlSize, 100, Integer.MAX_VALUE);

		txtDlCount = new Text(cpDl, SWT.BORDER | SWT.RIGHT);
		txtDlCount.setBounds(216, 67, 90, 23);
		MyListener.addNumberVerifyListener(txtDlCount, 1, 100);

		ckDlBySite = new Button(cpDl, SWT.CHECK);
		ckDlBySite.setToolTipText("保存图片时按站点分别创建文件夹");
		ckDlBySite.setSelection(true);
		ckDlBySite.setBounds(63, 114, 105, 17);
		ckDlBySite.setText("按站点保存图片");

		ckDlRename = new Button(cpDl, SWT.CHECK);
		ckDlRename.setToolTipText("保存图片为时间戳，相同地址图片会重复下载");
		ckDlRename.setText("自动重命名文件");
		ckDlRename.setBounds(63, 159, 105, 17);

		ckDlFast = new Button(cpDl, SWT.CHECK);
		ckDlFast.setToolTipText("边读边写模式");
		ckDlFast.setSelection(true);
		ckDlFast.setText("快速下载");
		ckDlFast.setBounds(63, 198, 69, 17);

		tabO = new TabItem(tabFolder, SWT.NONE);
		tabO.setText("其他(&O)");

		cpAdvance = new Composite(tabFolder, SWT.NONE);
		tabO.setControl(cpAdvance);

		checkVersion = new Button(cpAdvance, SWT.CHECK);
		checkVersion.setSelection(true);
		checkVersion.setText("版本检查");
		checkVersion.setBounds(106, 67, 121, 17);

		autoUpdate = new Button(cpAdvance, SWT.CHECK);
		autoUpdate.setText("自动更新");
		autoUpdate.setBounds(106, 130, 98, 17);

		update = new Button(cpAdvance, SWT.NONE);
		update.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		update.setText("立即更新(&U)");
		update.setBounds(255, 182, 80, 27);

		switch (ch) {
		case 'B':// 基本
			tabB.getControl().setFocus();
			tabFolder.setSelection(tabB);
			break;
		case 'C':// 缓存池
			tabFolder.setSelection(tabC);
			break;
		case 'S':// 扫描池
			tabFolder.setSelection(tabS);
			break;
		case 'D':// 下载池
			tabFolder.setSelection(tabD);
			break;
		case 'O':// 其他
			tabFolder.setSelection(tabO);
			break;
		}

		setting.addShellListener(new ShellAdapter() {
			@Override
			public void shellActivated(ShellEvent e) {
				txtDllDir.setText(Config.dlDir);
				txtCacheDir.setText(Config.cacheDir);
				txtCacheSize.setText(Config.cacheSize + "");
				txtCacheCount.setText(Config.cacheCount + "");
				ckCacheAutoClear.setSelection(Config.cacheAutoClear);
				txtScanSize.setText(Config.scanSize + "");
				txtScanCount.setText(Config.scanCount + "");
				ckScanDelFile.setSelection(Config.sacnDelFile);
				txtDlSize.setText(Config.dlSize + "");
				txtDlCount.setText(Config.dlcount + "");
				ckDlBySite.setSelection(Config.dlBysite);
				ckDlRename.setSelection(Config.dlRename);
				ckDlFast.setSelection(Config.dlFast);
				checkVersion.setSelection(Config.checkVersion);
				autoUpdate.setSelection(Config.autoUpdate);
			}
			@Override
			public void shellClosed(ShellEvent e) {
				Config.dlDir = txtDllDir.getText();
				Config.cacheDir = txtCacheDir.getText();
				Config.cacheSize = Integer.parseInt(txtCacheSize.getText());
				Config.cacheCount = Integer.parseInt(txtCacheCount.getText());
				Config.cacheAutoClear = ckCacheAutoClear.getSelection();
				Config.scanSize = Integer.parseInt(txtScanSize.getText());
				Config.scanCount = Integer.parseInt(txtScanCount.getText());
				Config.sacnDelFile = ckScanDelFile.getSelection();
				Config.dlSize = Integer.parseInt(txtDlSize.getText());
				Config.dlcount = Integer.parseInt(txtDlCount.getText());
				Config.dlBysite = ckDlBySite.getSelection();
				Config.dlRename = ckDlRename.getSelection();
				Config.dlFast = ckDlFast.getSelection();
				Config.checkVersion = checkVersion.getSelection();
				Config.autoUpdate = autoUpdate.getSelection();
			}
		});

		setting.open();
	}
}
